import subprocess
import time
from operator import truediv
import sys
import re
import shutil
import os
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from io import StringIO
print("    引入库完成,开始运行")


theTotalCount=1
tooSmall = 0

def moveTheFile(oldPath,toPath):
    global theTotalCount
    oldPath = oldPath.replace("\\", "\\\\")
    try : 
        shutil.move(oldPath, toPath)
    except FileNotFoundError: 
        print ('注意:未找到该文件\n' + oldPath) 
    except : 
        print ('注意:被其它程序占用或者目标文件已存在...\n' + oldPath)
    #print("    第"+ str(theTotalCount) + "个文件移动成功\n\n")
    theTotalCount += 1

#检测文件夹内是否有小于x大小KB的文件
def checkSmallThan10K(thecheckPath,theSize):
    global tooSmall
    folder_path_166 = thecheckPath
    file_names = os.listdir(folder_path_166)
    
    for file_name in file_names:
        #print("    获取完整路径")
        theFileFullPath = folder_path_166 + "\\" + file_name

        #print("    如果不是文件夹时才继续执行")
        if not os.path.isdir(theFileFullPath): 
            theFileSize=os.stat(theFileFullPath).st_size
            
            if(theFileSize < theSize):
                tooSmall += 1
                print(" " + str(theFileSize / 1000) + " KB:  " +  file_name  )
                #os.system("\"" +theFileFullPath + "\"")
                subprocess.run("\"" +theFileFullPath + "\"", shell=True)
    return

if len(sys.argv)>1:

    print("    托入:" + str(len(sys.argv)-1) +"个文件\n\n")

    #收集未转的文件名和数量
    theMatterFiles=[]
    #嵌入
    theTextCount=0 
    #未嵌入
    UnTextCount=0 
    #已转曲数量
    zhuanQuCount = 0
    
    
    
    for x in sys.argv[1:]:
        
        #存放文件名
        text=""

        #print(">页面计数",end = "")
        thePageCount=0

        #print("    第"+ str(theTotalCount) +"个打开:" + x)
        with open(x, 'rb') as file:

            #print("    创建PDF对象",end = "")
            resource_manager = PDFResourceManager()
        
            #print(">StringIO",end = "")
            output = StringIO()
        
            #print(">Converter",end = "")
            converter = TextConverter(resource_manager, output, laparams=LAParams())
        
            #print(">PDFPageInterpreter",end = "")
            interpreter = PDFPageInterpreter(resource_manager, converter)
            
            #为整个PDF文档建一个字体字符串数组
            fontArray = []

            #print(">逐页解析:")
            for page in PDFPage.get_pages(file):
                print(">",end = "",flush=True)
                interpreter.process_page(page)
                thePageCount +=1

                #print(f"{thePageCount}","中含有字体:",f"{len(interpreter.fontmap)}")

                if len(interpreter.fontmap) > 0:
                    for key in interpreter.fontmap:
                        theDes = interpreter.fontmap[key].descriptor
                        #如果字体数组中没有这个字体则继续执行
                        if interpreter.fontmap[key].fontname not in fontArray:
                            if "FontFile3" in theDes or "FontFile2" in theDes:
                                print("\n已嵌入  字体: ",interpreter.fontmap[key].fontname + "  ")
                                fontArray.append(interpreter.fontmap[key].fontname)
                                theTextCount += 1
                            else:
                                print("\n【未嵌入】    字体: ",interpreter.fontmap[key].fontname)
                                print(key, interpreter.fontmap[key].__class__.__name__,interpreter.fontmap[key].descriptor,"\n")
                                fontArray.append(interpreter.fontmap[key].fontname)
                                subprocess.run("\"" + x + "\"", shell=True)
                                theMatterFiles.append(x)
                                UnTextCount += 1
            
            if len(fontArray)>0:
                print("\n    含字体:",len(fontArray),"个    ",x,"\n-------------------------------------------------------------------------------------------------------\n")
            else:
                zhuanQuCount += 1
                    
            #print("    提取文本")
            text = output.getvalue()


print("\n\n共检测",str(len(sys.argv)-1),"个文件")

if(zhuanQuCount < len(sys.argv)-1):
    print("\n    其中已转曲文件有",str(zhuanQuCount),"个\n\n    未转曲文件有",str(len(sys.argv)-1-zhuanQuCount),"个")

if (theTextCount ) >0:
    if theTextCount >0:
        print("\n\n共发现 " + str(theTextCount) +" 款字体被嵌入")
    if UnTextCount >0:
        print("\n\n共发现 " + str(UnTextCount) +" 个【未嵌入】,不可印刷!!!!!")
    else:
        print("\n    虽未转曲,全已嵌入,可以印刷")#1秒后自动结束
        #time.sleep(1)
else:
    print("\n    全已转曲,可以直接印刷")

input()











